Imports System.IO
Imports System.Collections.Generic
Imports System.Xml.XPath

Public Class InformationRequest
    Inherits Request

#Region "shared"

    Private Shared ReadOnly XPATH_LANGUAGE As XPathExpression = XPathExpression.Compile("/request/item[@name = 'internationalization']")
    Private Shared ReadOnly XPATH_SESSION_ID As XPathExpression = XPathExpression.Compile("/request/item[@name = 'session-id']")
    'Private Shared ReadOnly XPATH_KEY1 As XPathExpression = XPathExpression.Compile("/request/item[@name = 'key 1']")
    'Private Shared ReadOnly XPATH_KEY2 As XPathExpression = XPathExpression.Compile("/request/item[@name = 'key 2']")
    'Private Shared ReadOnly XPATH_KEYS As XPathExpression = XPathExpression.Compile("/request/item[start-with(@name, 'key ')]")
    Private Shared ReadOnly XPATH_KEYS As New List(Of XPathExpression)

    Private Shared Function GetLanguage(ByVal xn) As String

        Dim xnX As XPathNavigator = xn.SelectSingleNode(XPATH_LANGUAGE)
        If xnX Is Nothing Then
            Throw New ApplicationException("language code expected")
        End If

        Return xnX.Value

    End Function

    Private Shared Function GetSessionId(ByVal xn) As String

        Dim xnX As XPathNavigator = xn.SelectSingleNode(XPATH_SESSION_ID)
        If xnX Is Nothing Then
            Throw New ApplicationException("session id expected")
        End If

        Return xnX.Value

    End Function
#End Region

#Region "instance"

    Private _Language As String
    Private _SessionId As String
    Private _Keys As New List(Of String)

    Public Sub New(ByVal xn As XPathNavigator, ByVal xmlStr As String)

        MyBase.New(xn, RequestType.Information, xmlStr)

        _Language = GetLanguage(xn)
        _SessionId = GetSessionId(xn)

        Dim index As Integer = 1
        Do
            Dim key_xpath As XPathExpression
            If index <= XPATH_KEYS.Count Then
                key_xpath = XPATH_KEYS(index - 1)
            Else
                key_xpath = XPathExpression.Compile("/request/item[@name = 'key " & index & "']")
                XPATH_KEYS.Add(key_xpath)
            End If

            Dim key_xn As XPathNavigator = xn.SelectSingleNode(key_xpath)
            If key_xn Is Nothing Then
                Exit Do
            End If

            Dim key_value As String = key_xn.Value
            _Keys.Add(key_value)

            index += 1
        Loop
    End Sub

    Public ReadOnly Property Language() As String
        Get
            Return _Language
        End Get
    End Property

    Public ReadOnly Property SessionId() As String
        Get
            Return _SessionId
        End Get
    End Property

    Public ReadOnly Property Keys() As List(Of String)
        Get
            Dim lst As List(Of String)
            If _Keys.Count > 0 Then
                lst = _Keys.GetRange(0, _Keys.Count - 1)
            Else
                lst = New List(Of String)
            End If

            Return lst
        End Get
    End Property

    Protected Overrides Function DoResponse(ByVal r As System.Net.HttpWebResponse) As Response

        Dim resp As Response

        If r.ContentLength = 0 Then
            Throw New ApplicationException("content expected")
        End If

        Dim os As Stream = r.GetResponseStream()
        'Using sr As New StreamReader(os)
        '    Dim xml As String = sr.ReadToEnd()
        '    resp = New InformationResponse(xml)
        'End Using
        resp = New InformationResponse(os)
        os.Close()

        Return resp

    End Function
#End Region
End Class
